<?php



class Action
{
    function __call($width = 0, $param = null)
    {
        $file = _GET('file');
        $width = intval($width);
        if (isset($param[0])) {
            $height = intval($param[0]);
        } else {
            $height = 0;
        }
        $file = ROOT_PATH.'/'.$file;

        if (!file_exists($file)) {
            echo 1;
            exit;
        }
        if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) - time() < 60)) {
            header("HTTP/1.1 304 Not Modified", true);
            exit;
        }
        
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        

        $width > 1000 && $width = 1000;
        $height > 1000 && $height = 1000;
        $width == 0 && $width = 100;

        $im = new Imagick();
        try {
            if (!$im->readImage($file)) {
                echo 2;
                exit;
            }
        } catch (Exception $e) {
            print_r($e);
            exit;
        }


        if (empty($height)) {
            //if ($im->getImageWidth() > $width) {
            $im->thumbnailImage($width, null);
            //}
        } else {
            $im->cropThumbnailImage($width, $height);
        }
        header("Content-Type: image/{$im->getImageFormat()}");
        header("Expires: Fri, 12 Nov 2010 10:42:29 GMT");
        echo $im;
        exit;
    }
}


?>